package com.example.demo.bingfa;

import jdk.internal.dynalink.beans.StaticClass;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @ClassName TestDeadLock
 * @Description TODO
 * Author yangqinglin
 * @Date 2021/2/19 16:07
 * @Version
 **/

@Slf4j(topic = "TestDeadLock")
public class TestDeadLock {

    static List<String> MENU= Arrays.asList("宫保鸡丁","鱼香肉丝","酸菜鱼","番茄炒鸡蛋");
    static Random RANDOM=new Random(0);
    static String cooking(){
        return  MENU.get(RANDOM.nextInt(MENU.size()));
    }


    public static void main(String[] args) {
        ExecutorService waitpool= Executors.newFixedThreadPool(1);
        ExecutorService cookpool= Executors.newFixedThreadPool(1);

        waitpool.execute(()->{
             log.debug("点菜");
            Future<String>  f=cookpool.submit(()->{
                 log.debug("做菜");

                 return cooking();
             });


            try{
                log.debug("上菜：{}",f.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });


        waitpool.execute(()->{
            log.debug("点菜");
            Future<String>  f=cookpool.submit(()->{
                log.debug("做菜");

                return cooking();
            });


            try{
                log.debug("上菜：{}",f.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
    }
}
